package com.zyuan.lawyerport;

import android.app.Dialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.zyuan.lawyerport.presenter.guild.NetImageHolderView;
import com.zyuan.lawyerport.presenter.guild.NhvBean;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import common.tool.PUB;

/**
 * 第一次安装引导界面
 */
public class GuildActivity extends AppCompatActivity {
    @BindView(R.id.wellcome_convenient)
    ConvenientBanner wellcomeConvenient;

    private List<NhvBean> mList;//展示的图片集合
    private String firstInIdentifying;//是否为安装后app第一次进入的标识
    private String getVersionCode;//获取当前app版本信息
    private Dialog selectorDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //取消标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //取消状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_guild);
        ButterKnife.bind(this);

        try {
            init();
        } catch (Exception ex) {
            Logger.e(ex.toString());
        }
    }

    /**
     * 是否展示滑动介绍图片
     */
    private void init() {
        try {
            getVersionCode = PUB.getVersionCode(this);
        } catch (Exception ex) {
            Logger.e(ex.toString());
        }
        firstInIdentifying = PUB.SharedPreferences(this, "firstInIdentifying", "#read");
        if (getVersionCode.equals(firstInIdentifying)) {
            String login_mobile = PUB.SharedPreferences(getApplicationContext(), "login_mobile", "#read");
            String login_password = PUB.SharedPreferences(getApplicationContext(), "login_password", "#read");
            if (!TextUtils.isEmpty(login_mobile) && !TextUtils.isEmpty(login_password)) {
                //自动登录
                initLogin(login_mobile, login_password);
            } else {
                //数据一致，直接进入
                startActivity(new Intent(GuildActivity.this, MainActivity.class));
                finish();
            }

        } else {
            //不一致，加载展示界面
            initPageView();
        }
    }


    /**
     * 登录
     */
    private void initLogin(final String mobile, final String password) {
//        //splash
//        LayoutInflater inflater = getLayoutInflater();
//        View dialog = inflater.inflate(R.layout.loading_spinkit, null);
//        selectorDialog = new Dialog(this, R.style.CustomDialog);
//        selectorDialog.setContentView(dialog);
//
//        if (mobile.length() != 11) {
//            Toasty.error(this, "手机号码位数不正确").show();
//
//        } else if (TextUtils.isEmpty(password)) {
//            Toasty.error(this, "密码不能为空").show();
//
//        } else {
//            if (selectorDialog != null) {
//                selectorDialog.show();
//            }
//
//            new MemberOP(this).LoginAction(new OkGoFinishAllListener() {
//                @Override
//                public void onSuccess(String mStr, Object bean) {
//                    if (selectorDialog != null) {
//                        selectorDialog.dismiss();
//                    }
//
//                    LoginActionBean mBean = (LoginActionBean) bean;
////                    Toasty.normal(getApplicationContext(), mBean.getMessage()).show();
//                    if (mBean.getErrcode() == 1) {
//                        PUB.SharedPreferences(getApplicationContext(), "login_mobile", mobile);
//                        PUB.SharedPreferences(getApplicationContext(), "login_password", password);
//
//                        Intent mIntent = new Intent(getApplicationContext(), MainFragmentActivity.class);
//                        Bundle mBundle = new Bundle();
//                        mBundle.putSerializable("DataBean", mBean);
//                        mIntent.putExtras(mBundle);
//                        startActivity(mIntent);
//
//                        finish();
//                    } else if (mBean.getErrcode() == 2) {
//                        PUB.showNormalDialogOne(WellcomeActivity.this, "温馨提示", "您的账号已被锁定", new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialog, int which) {
//                                Intent mIntent = new Intent(WellcomeActivity.this, MainFragmentActivity.class);
//                                startActivity(mIntent);
//                                finish();
//                            }
//                        });
//                    } else {
//                        Toasty.normal(getApplicationContext(), mBean.getMessage()).show();
//                        PUB.SharedPreferences(getApplicationContext(), "login_password", "");
//                        Intent mIntent = new Intent(WellcomeActivity.this, LoginActivity.class);
//                        startActivity(mIntent);
//                        finish();
//                    }
//                }
//
//                @Override
//                public void onError(String mStr) {
//                    if (selectorDialog != null) {
//                        selectorDialog.dismiss();
//                    }
//
//                    Intent mIntent = new Intent(WellcomeActivity.this, LoginActivity.class);
//                    startActivity(mIntent);
//                    finish();
//
//                }
//            }, mobile, password);
//
//        }
    }


    /**
     * 初始化数据及展示滑动介绍图片
     */
    private void initPageView() {
        mList = new ArrayList();
        mList.add(new NhvBean(1,R.mipmap.ico_left));
        mList.add(new NhvBean(2,R.mipmap.ic_launcher));
        mList.add(new NhvBean(3,R.mipmap.ico_right));

        wellcomeConvenient.setPages(new CBViewHolderCreator() {
            @Override
            public Holder createHolder(View itemView) {
                return new NetImageHolderView(itemView, GuildActivity.this,getVersionCode,wellcomeConvenient);
            }

            @Override
            public int getLayoutId() {
                return R.layout.item_guild;
            }
        }, mList)
                //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器，不需要圆点指示器可以不设
                .setPageIndicator(new int[] { R.drawable.banner_shape_unselect,
                        R.drawable.banner_shape_select})
                //设置指示器的方向
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                //设置指示器是否可见
                .setPointViewVisible(true);
//                //监听单击事件
//                .setOnItemClickListener(this)

        wellcomeConvenient.setCanLoop(false);
    }



}

